/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import ejb.Calendarejb;
import entity.Event;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.ScheduleEvent;

/**
 *
 * @author user
 */

@Named
@SessionScoped


public class Eventshowbean implements Serializable{
    
    @Inject
    private Calendarejb calejb;
    
    @Inject
    private Calendarbean calendarbean;
    
    private Event event;
    
    private List<Event> events;
    
    private String eventsearch;
    
    private HtmlDataTable datatable;

    public HtmlDataTable getDatatable() {
        return datatable;
    }

    public void setDatatable(HtmlDataTable datatable) {
        this.datatable = datatable;
    }

    public String getEventsearch() {
        return eventsearch;
    }

    public void setEventsearch(String eventsearch) {
        this.eventsearch = eventsearch;
    }

    public Event getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }
    
    
    public void onEventSelect(SelectEvent selectEvent) {

        ScheduleEvent scheduleevent = (ScheduleEvent) selectEvent.getObject();
        event = calejb.findEventbyName(scheduleevent.getTitle(), scheduleevent.getStartDate());
        System.out.println(scheduleevent.getTitle());
        System.out.println(event.getName());

        ConfigurableNavigationHandler configurableNavigationHandler = (ConfigurableNavigationHandler) FacesContext.getCurrentInstance().getApplication().getNavigationHandler();
        configurableNavigationHandler.performNavigation("/s/user/showevent?faces-redirect=true");
//        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
//        try {
//            context.redirect(context.getRequestContextPath() + "/s/user/showevent.xhtml?faces-redirect=true");
//
//        } catch (IOException ex) {
//
//        }
    }
    
    public List<Event> searchAllEvents() {
        
        return events;
        
    }
    
    public String gotoSearch(){
        
        System.out.println("eventsearch contains: " + this.eventsearch);
        
        events = calejb.getAllEvents(this.eventsearch);
        
        this.eventsearch=null;
        
        return "/s/user/search?faces-redirect=true";
    }
    
    public String showEvent(){
        
        event = (Event) datatable.getRowData();
        
        return "/s/user/showevent?faces-redirect=true";
        
    }
}
